package com.xs.shop.marketing.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xs.shop.common.PageResult;
import com.xs.shop.common.Result;
import com.xs.shop.marketing.model.entity.Participation;
import com.xs.shop.marketing.service.service.ParticipationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/participation")
@Tag(name = "活动参与管理", description = "活动参与的CRUD操作")
public class ParticipationController {

    @Resource
    private ParticipationService participationService;

    @PostMapping("add")
    @Operation(summary = "新增参与记录", description = "创建一条新的活动参与记录")
    public Result<Participation> addParticipation(@RequestBody Participation participation) {
        Participation savedParticipation = participationService.addParticipation(participation);
        return Result.success(savedParticipation);
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除参与记录", description = "根据ID删除活动参与记录")
    public Result<Boolean> deleteParticipation(@PathVariable @Parameter(description = "参与记录ID") Long id) {
        boolean result = participationService.deleteParticipation(id);
        return Result.success(result);
    }

    @PutMapping("update")
    @Operation(summary = "更新参与记录", description = "更新活动参与记录信息")
    public Result<Participation> updateParticipation(@RequestBody Participation participation) {
        Participation updatedParticipation = participationService.updateParticipation(participation);
        return Result.success(updatedParticipation);
    }

    @GetMapping("{id}")
    @Operation(summary = "获取参与详情", description = "根据ID获取活动参与详情")
    public Result<Participation> getParticipationById(@PathVariable @Parameter(description = "参与记录ID") Long id) {
        Participation participation = participationService.getParticipationById(id);
        return Result.success(participation);
    }

    @GetMapping("page")
    @Operation(summary = "分页查询参与记录", description = "分页查询活动参与记录，支持多条件查询")
    public PageResult<Participation> pageParticipation(
            @RequestParam(defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(required = false) @Parameter(description = "活动ID") Long activityId,
            @RequestParam(required = false) @Parameter(description = "会员ID") Long memberId,
            @RequestParam(required = false) @Parameter(description = "优惠券ID") Long couponId,
            @RequestParam(required = false) @Parameter(description = "参与状态") Integer status
    ) {
        IPage<Participation> page = participationService.pageParticipation(
                current, pageSize, activityId, memberId, couponId, status);
        return PageResult.success(page);
    }

    @GetMapping("userParticipateActivityAndGetCoupon")
    @Operation(summary = "用户参与活动并获得优惠券", description = "根据传入的活动id与优惠券id，记录当前用户参与活动，并发放对应的优惠券")
    public Result<Boolean> userParticipateActivityAndGetCoupon(@RequestParam Long activityId,@RequestParam Long couponId){
        Integer res = this.participationService.userParticipateActivityAndGetCoupon(activityId, couponId);
        return Result.success( res == 1? true: false );
    }
}

